﻿using ASPNETMVC4Tutorial.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ASPNETMVC4Tutorial.Controllers
{
    public class HomeController : Controller
    {
        private EFContext db = new EFContext();

        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            LN.Framework.LNLog.Tracer.Info("HomeController.Index");
            //return View();

            // Get most popular albums
            var albums = GetTopSellingAlbums(5);

            return View(albums);

        }

        private List<Album> GetTopSellingAlbums(int count)
        {
            try
            {
                return db.Albums.ToList();       
            }
            catch (Exception e)
            {

                return null;
            }
                   

        }

        [NoCache]
        public string GetCurrentUtcTime()
        {
            return DateTime.UtcNow.ToLongTimeString();
        }

        //通过定制响应 Cache-Control 头解决ajax缓存问题
        public class NoCacheAttribute : FilterAttribute, IActionFilter
        {
            public void OnActionExecuted(ActionExecutedContext filterContect)
            {
                filterContect.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            }

            public void OnActionExecuting(ActionExecutingContext filterContext)
            {

            }
        }
    }
}
